####
#### Eroll Kuhn & Rahsaan Maxwell
#### Main Plot
### 10.2020


rm(list=ls())

library(tidyverse)
library(gridExtra)

# read in frame
df <- read.csv(".../5q.Robustness, Leave-One-Out CoB/Output/B.Tables/TableForPlot.csv")


# pre-processing
df <- data.frame(df)

#df <- subset(df,df$CoB.Excluded!=23)
df <- subset(df, df$Variable !="% from Arabic-Speaking Countries")


df$facet_lab <- ifelse(df$Variable=="% Foreign-Born",1,
                       ifelse(df$Variable=="% Non-EU Foreign-Born",2,
                              #ifelse(df$Variable=="% from Arabic-Speaking Countries",3,
                                     ifelse(df$Variable=="% Co-National",3,NA)))

df$facet_lab <- factor(df$facet_lab,
                    levels = c(1,2,3),
                    labels = c("% Foreign-Born", 
                               "% Non-EU Foreign-Born",
                               "% Co-National"))


png("/5q.Robustness, Leave-One-Out CoB/Output/D.Figures/LOO.png",
    width=10,height=12,units='in',res=300)
ggplot(df) +
  geom_pointrange(aes(x=CoB.Excluded,y=Coef,ymin=Lower95,ymax=Upper95),lwd=0.25,fatten = .2) +
  geom_pointrange(aes(x=CoB.Excluded,y=Coef,ymin=Lower90,ymax=Upper90),lwd=0.5,size=.2,fatten = .2) +
  facet_wrap(~facet_lab,nrow=1) +
  theme_bw() +
  geom_vline(xintercept = 21.5, lty = 2,col="red") +
  geom_hline(yintercept = 0,lty=2) +
  ylab("Effect on Demographic Context on Feeling Welcome") +
  xlab("National Origin Group Excluded") +
  theme(legend.direction = "horizontal",
        legend.position = "bottom",
        panel.grid.major.x = element_blank(),
        panel.grid.minor = element_blank(),
        panel.grid.major = element_blank(),
        axis.text.x = element_text(size=8, angle = 15, hjust = 1),
        axis.text.y = element_text(size=8),
        legend.text = element_text(size = 12)) +
scale_x_discrete(limits=c(factor(seq(1:23))),
                 labels = c("1"="Afghanistan",
                              "2"="Albania",
                              "3"="Algeria",
                              "4"="Armenia",
                              "5"="Bosnia",
                              "6"="Eritrea",
                              "7"="Gambia",
                              "8"="Georgia",
                              "9"="India",
                              "10"="Iraq",
                              "11"="Iran",
                              "12"="Kosovo",
                              "13"="Macedonia",
                              "14"="Nigeria",
                              "15"="Pakistan",
                              "16"="Russia",
                              "17"="Serbia",
                              "18"="Somalia",
                              "19"="Syria",
                              "20"="Ukraine",
                              "21"="Other Nationality",
                              "22" ="Retain All",
                              "23" = "Retain Only Syria")) + 
  coord_flip()
dev.off()   
